package com.coder.bike.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coder.bike.entity.BikeInfo;
import com.coder.bike.mapper.BikeInfoMapper;
import com.coder.bike.service.IBikeInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 沈诩宸
 * @since 2025-04-22
 */
@Service
public class BikeInfoServiceImpl extends ServiceImpl<BikeInfoMapper, BikeInfo> implements IBikeInfoService {
    @Override
    public Page<BikeInfo> searchByPage(Page<BikeInfo> page, BikeInfo bikeInfo) {
        return baseMapper.searchByPage(page, bikeInfo);
    }

    @Override
    public boolean delete(String ids) {
        List<Integer> list = Arrays.stream(ids.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        if (!list.isEmpty()) {
            return baseMapper.deleteBatchIds(list) > 0;
        }
        return false;
    }

    @Override
    public Map<String, Integer> getStatusCount() {
        return baseMapper.getStatusCount();
    }
    @Override
    public BikeInfo getByBikeId(String bikeId) {
        return baseMapper.selectByBikeId(bikeId);
    }

    @Override
    public List<BikeInfo> toBeMaintained() {
        // 查询超过3个月未维护的单车
        LocalDateTime threeMonthsAgo = LocalDateTime.now().minusMonths(3);
        return baseMapper.selectToBeMaintained(threeMonthsAgo);
    }
}
